<!DOCTYPE html>
<html lang="en" class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>最新资讯配置</title>
    <header th:replace="header::html"></header>
</head>

<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a><cite>首页</cite></a>
            <a><cite>门户管理</cite></a>
            <a><cite>最新资讯配置</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>

<div class="layui-fluid">
    <div class="layui-row  layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form  layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="ltitle" placeholder="请输入标题关键字" autocomplete="off"
                                   class="layui-input">
                        </div>

                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="test20" placeholder="请选择资讯的获取日期"
                                       name="getInfoDate">
                            </div>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="search"><i
                                    class="layui-icon">&#xe615;</i></button>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button type="button" class="layui-btn layui-btn-fluid" onclick="getLatest()">--获取今日最新资讯--
                            </button>
                        </div>
                    </form>
                </div>


                <div class="layui-card-body ">
                    <table class="layui-hide" id="latestInfo" lay-filter="latestInfo"></table>
                </div>

                <div class="layui-card-body ">
                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="open"
                        >发布</a>
                        <a class="layui-btn layui-btn-xs" lay-event="close"
                        >下架</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="edit"
                        >编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"
                        >删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>

	layui.use('laydate', function () {
		var laydate = layui.laydate;
		//选中后的回调
		laydate.render({
			elem: '#test20'
			, done: function (value, date) {
				//layer.alert('你选择的日期是：' + value + '<br>获得的对象是' + JSON.stringify(date));
			}
		});
	});


	layui.use('table', function () {
		var table = layui.table,
			form = layui.form;
		table.render({
			elem: '#latestInfo'
			, url: '/api-back/latestInfo/list' //数据接口
			, page: true //开启分页
			, cellMinWidth: 80 //全局定义常规单元格的最小宽度
			, response: {
				dataName: 'datas' //规定数据列表的字段名称，默认：data
			}
			, cols: [
				[ //表头
					{type: 'checkbox', fixed: 'left'}
					, {title: '序号', type: 'numbers', width: '5%'}
					, {field: 'lid', title: 'ID', align: 'center', sort: true, width: "5%", style: 'display:none;'}
					, {field: 'lcontent', title: '源头', align: 'center'}
					, {field: 'ltitle', title: '资讯标题', sort: true, width: "25%"}
					, {field: 'lurl', title: 'URL', align: 'center'}
					, {field: 'tips1', title: '获取时间', align: 'center', width: "8%"}
					, {field: 'lwho', title: '发布者', align: 'center', width: "5%"}
					, {field: 'ltime', title: '发布时间', align: 'center', width: "8%"}
					, {title: '操作', toolbar: '#barDemo', align: 'center', width: "20%"}
				]
			],
			done: function () {
				//checkPermission();
				$('table.layui-table thead tr th:eq(2)').addClass('layui-hide');//隐藏第一列的表头
			}
		});

		//监听工具条
		table.on('tool(latestInfo)', function (obj) {
			var data = obj.data;//获取点击行数据
			if (obj.event === 'del') {
				layer.confirm('确认删除该条资讯吗?', function (index) {
					$.ajax({
						url: "/api-back/latestInfo/delete",
						type: "GET",
						data: {lid: data.lid},
						dataType: 'json',
						success: function (result) {
							layer.alert("删除成功", {icon: 1}, function (index1) {
								layer.close(index1);
								table.reload('latestInfo');
								$('table.layui-table thead tr th:eq(2)').addClass('layui-hide');//隐藏第一列的表头
							});
						},
						error: function () {
							layer.open({
								type: 2,
								content: "/404.html",
								area: ['60%', '60%'],
								title: '权限不足'
							});
						}
					});
				});
			} else if (obj.event === 'open') {
				layer.confirm('确认发布该条资讯到门户吗?(TIPS:您可以发布多条资讯,但是您所发布的资讯将取前十条按时间倒序展示在主页当中)', function (index) {
					if (data.lwho == null || data.lwho == '') {
						$.ajax({
							url: "/api-back/latestInfo/open",
							type: "GET",
							data: {lid: data.lid},
							dataType: 'json',
							success: function (result) {
								layer.alert("发布成功", {icon: 1}, function (index1) {
									layer.close(index1);
									table.reload('latestInfo');
									$('table.layui-table thead tr th:eq(2)').addClass('layui-hide');//隐藏第一列的表头
								});
							},
							error: function () {
								layer.open({
									type: 2,
									content: "/404.html",
									area: ['60%', '60%'],
									title: '权限不足'
								});
							}
						});
					} else {
						layer.alert("已发布的需求无法重复发布，请确认！", {icon: 2})
					}

				});
			} else if (obj.event === 'close') {
				layer.confirm('确认下架该条资讯吗?', function (index) {
						if (data.lwho !== null && data.lwho !== '') {
							$.ajax({
								url: "/api-back/latestInfo/close",
								type: "GET",
								data: {lid: data.lid},
								dataType: 'json',
								success: function (result) {
									layer.alert("下架成功", {icon: 1}, function (index1) {
										layer.close(index1);
										//xadmin.father_reload();
										table.reload('latestInfo');
										$('table.layui-table thead tr th:eq(2)').addClass('layui-hide');//隐藏第一列的表头
									});
								},
								error: function () {
									layer.open({
										type: 2,
										content: "/404.html",
										area: ['60%', '60%'],
										title: '权限不足'
									});
								}
							});
						} else {
							layer.alert("已发布的需求才可以下架，请确认！", {icon: 2})
						}
					}
				);
			} else if (obj.event === 'edit') {
				layer.prompt({title: '请输入新的资讯标题，并确认', formType: 2}, function (pass, index) {

					if (pass.trim().length >= 20 || pass.trim().length < 5) {
						layer.msg("对不起，字数请限制在5到20位，请检查！");
					} else {
						$.ajax({
							url: "/api-back/latestInfo/edit",
							type: "GET",
							data: {
								lid: data.lid,
								ltitle: pass
							},
							dataType: 'json',
							success: function (result) {
								layer.alert("修改成功", {icon: 1}, function (index1) {
									layer.close(index1);
								});
							},
							error: function () {
								layer.open({
									type: 2,
									content: "/404.html",
									area: ['60%', '60%'],
									title: '权限不足'
								});
							}
						});
						layer.close(index);
						table.reload('latestInfo');
						$('table.layui-table thead tr th:eq(2)').addClass('layui-hide');//隐藏第一列的表头
					}
				});
			}
		});

		//搜索
		form.on('submit(search)', function (data) {
			var field = data.field;
			table.render({
				elem: '#latestInfo'
				, url: '/api-back/latestInfo/findLatestInfoByFuzzy' //数据接口
				, page: true //开启分页
				, where: {
					"ltitle": field.ltitle,
					"getDate": field.getInfoDate
				}
				, response: {
					dataName: 'datas' //规定数据列表的字段名称，默认：data
				}
				, cols: [
					[ //表头
						{type: 'checkbox', fixed: 'left'}
						, {title: '序号', type: 'numbers', width: '5%'}
						, {field: 'lid', title: 'ID', align: 'center', sort: true, width: "5%", style: 'display:none;'}
						, {field: 'lcontent', title: '源头', align: 'center'}
						, {field: 'ltitle', title: '资讯标题', sort: true, width: "25%"}
						, {field: 'lurl', title: 'URL', align: 'center'}
						, {field: 'tips1', title: '获取时间', align: 'center', width: "8%"}
						, {field: 'lwho', title: '发布者', align: 'center', width: "5%"}
						, {field: 'ltime', title: '发布时间', align: 'center', width: "8%"}
						, {title: '操作', toolbar: '#barDemo', align: 'center', width: "20%"}
					]
				],
				done: function () {
					//checkPermission();
					$('table.layui-table thead tr th:eq(2)').addClass('layui-hide');//隐藏第一列的表头
				},
				error: function () {
					layer.open({
						type: 2,
						content: "/404.html",
						area: ['60%', '60%'],
						title: '权限不足'
					});
				}
			});
			return false;
		});

	});

	function getLatest() {
		layer.confirm('确认获取最新资讯吗?', function (index) {
			$.ajax({
				url: "/api-back/worm/spiter",
				type: "GET",
				success: function (result) {
					layer.alert("获取成功", {icon: 1}, function (index1) {
						layer.close(index1);
						xadmin.father_reload();
					});
				},
				error: function () {
					layer.open({
						type: 2,
						content: "/404.html",
						area: ['60%', '60%'],
						title: '权限不足'
					});
				}
			});
		});
	}


</script>
</html>